package com.usf.jprovider.schema.model.jmodel;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.usf.jprovider.object.JObject;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.schema.model.AbstractModel;
import com.usf.jprovider.schema.model.jmodel.item.collection.Table;

@XmlRootElement(name="model", namespace="http://www.w3.org/2001/XMLSchema-instance")

public final class JModel extends AbstractModel {
	
	@XmlElement(name="table") protected Table[] tables;
	
	public JModel(){
		super();
		tables= new Table[]{};
	}
	public Table[] getTable() {
		return tables;
	}
	public JObject create(Environment env) throws Exception {
		JObject object = new JObject(this.name);
		for(Table table : tables)
			table.create(env, object);
		return object;
	}
	public boolean check(Environment env, JObject object) throws Exception {
		for(Table table : tables)
			if(!table.check(env, object)) return false;
		return true;
	}
	public JObject persist(Environment env, JObject object)  throws Exception {
		for(Table table : tables)
			table.persist(env, object, null);
		env.getConnection().commit();
		return object;
	}
}
